package com.ruoyi.quality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.quality.domain.InspectionTaskValue;
import com.ruoyi.quality.domain.Sampling;
import com.ruoyi.quality.domain.dto.InspectionTaskValueDetailDto;
import com.ruoyi.quality.domain.dto.InspectionTaskValueDto;
import com.ruoyi.quality.mapper.InspectionTaskMapper;
import com.ruoyi.quality.service.InspectionTaskValueService;
import com.ruoyi.quality.mapper.InspectionTaskValueMapper;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class InspectionTaskValueServiceImpl implements InspectionTaskValueService{


    @Resource
    InspectionTaskValueMapper inspectionTaskValueMapper;


    @Override
//    @Transactional
    public void addInspectionTaskValues(List<InspectionTaskValueDetailDto> inspectionTaskValueDetailDtos) {
        String userName = SecurityUtils.getUsername();
        //数据全删和全插入
        List<Long> inspectionTaskTemplateSamplingItemIds = inspectionTaskValueDetailDtos.stream().map(item -> item.getId()).collect(Collectors.toList());
        MPJLambdaWrapper<InspectionTaskValue> wrapper = new MPJLambdaWrapper<InspectionTaskValue>()
                .in(InspectionTaskValue::getInspectionTaskTemplateSamplingItemId, inspectionTaskTemplateSamplingItemIds)
                .ne(Sampling::getDelFlag, 1);
        inspectionTaskValueMapper.delete(wrapper);

        /**
         * 数据保留
         */
        inspectionTaskValueMapper.deleteBatchIds(inspectionTaskTemplateSamplingItemIds);
        for(InspectionTaskValueDetailDto inspectionTaskValueDetailDto : inspectionTaskValueDetailDtos) {
            for(InspectionTaskValueDto inspectionTaskValueDto : inspectionTaskValueDetailDto.getValueList()) {
                InspectionTaskValue inspectionTaskValue = new InspectionTaskValue();
                inspectionTaskValue.setInspectionTaskTemplateSamplingItemId(inspectionTaskValueDetailDto.getId());
                inspectionTaskValue.setInspectionTaskId(inspectionTaskValueDetailDto.getInspectionTaskId());
                inspectionTaskValue.setName(inspectionTaskValueDto.getName());
                inspectionTaskValue.setLowerTolerance(inspectionTaskValueDto.getLowerTolerance());
                inspectionTaskValue.setUpperTolerance(inspectionTaskValueDto.getUpperTolerance());
                inspectionTaskValue.setValue(inspectionTaskValueDto.getValue());
                inspectionTaskValue.setValue2(inspectionTaskValueDto.getValue2());
                inspectionTaskValue.setType(inspectionTaskValueDto.getType());
                inspectionTaskValue.setCreateBy(userName);
                inspectionTaskValue.setCreateTime(new Date());
                inspectionTaskValue.setDelFlag(false);
                inspectionTaskValueMapper.insert(inspectionTaskValue);
            }
        }



    }
}




